package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.Staff;
import com.tms.domain.User;
import com.tms.domain.Workcell;
import com.tms.domain.index.UserInfo;
import com.tms.mapper.develop.UserMapper;
import com.tms.mapper.staff.StaffMapper;
import com.tms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户业务实现
 *
 * @author ozj
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Override
    @Cacheable(cacheNames = "users", key = " #no + #workcellId", unless = "#result == null")
    public User findUserByNoAndWorkcell(String no, int workcellId) {
        return userMapper.selectByPrimaryKey(no, workcellId);
    }

    @Override
    @Cacheable(cacheNames = "users", key = "#id", unless = "#result == null")
    public User findUserById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    @Cacheable(cacheNames = "staffs", key = "#id", unless = "#result == null")
    public Staff findStaffById(int id) {
        return staffMapper.selectById(id);
    }

    @Override
    @Cacheable(cacheNames = "staffs", key = "#no", unless = "#result == null")
    public Staff findStaffByNo(String no) {
        return staffMapper.selectByNo(no);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "userList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "userInfo", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "users", key = "#result.id", unless = "#result == null || #result.id == 0"),
                    @CachePut(cacheNames = "users", key = "#user.no + #user.workcell.id", unless = "#result == null || #result.id == 0")
            }
    )
    public User createUser(User user) {
        userMapper.insertSelective(user);
        return user;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "users", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "userList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "userInfo", allEntries = true, beforeInvocation = true)
            }
    )
    public void deleteUserByNoAndWorkcellId(String no, int workcellId) {
        userMapper.deleteByNo(no, workcellId);
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "users", key = "#user.id", unless = "#result == null"),
                    @CachePut(cacheNames = "users", key = "#user.no + #user.workcell.id", unless = "#result == null")
            },
            evict = {
                    @CacheEvict(cacheNames = "userList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "userInfo", allEntries = true, beforeInvocation = true)
            }
    )
    public User updateUserById(User user) {
        int result = userMapper.updateByPrimaryKeySelective(user);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return user;
    }

    @Override
    @Cacheable(cacheNames = "userList", key = "#page + #pageSize + #workcell.id", unless = "#result == null")
    public PageInfo<User> findUsersByWorkcellId(int page, int pageSize, Workcell workcell) {
        PageHelper.startPage(page, pageSize);
        List<User> list = userMapper.selectByWorkcell(workcell);
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(cacheNames = "userInfo", unless = "#result == null")
    public List<UserInfo> getUserInfo() {
        List<UserInfo> list = userMapper.selectPositionNum();
        for (UserInfo userInfo : list) {
            //去掉ROLE_，只选择后面的职位名称
            userInfo.setX(userInfo.getX().split("_")[1]);
        }
        return list;
    }
}